#include "ipset.h"
#include "ui_ipset.h"

IPSet::IPSet(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IPSet)
{
    ui->setupUi(this);

    QRegExp IPAddressRegExpression("((([1-9]?|1\d)\d|2([0-4]\d|5[0-5]))\.){3}(([1-9]?|1\d)\d|2([0-4]\d|5[0-5]))");
    QRegExp PortRegExpression("/^(\d|[1-5]\d{4}|6[1-4]\d{3}|65[1-4]\d{2}|655[1-2]\d|6553[1-5])$/");
    QRegExpValidator IPAddressRegExpressionValidator(IPAddressRegExpression);
    QRegExpValidator PortRegExpressionValidator(PortRegExpression);

    ui->ui_LineEditIPAddress->setValidator(&IPAddressRegExpressionValidator);
    ui->ui_LineEditPort->setValidator(&PortRegExpressionValidator);

    QSettings *config;
    try {
       config = new QSettings("./Config/Config.ini", QSettings::IniFormat);
    } catch (std::bad_alloc) {
        QMessageBox::critical(this, "错误", "申请内存空间失败!", QMessageBox::Yes);

        delete config;
        qApp->exit(-1);
    }
    if(config->contains("DATA/IP") && config->contains("DATA/PORT"))
    {
        ui->ui_LineEditIPAddress->setText(config->value("DATA/IP").toString());
        ui->ui_LineEditPort->setText(config->value("DATA/PORT").toString());
    }
    delete config;
}

IPSet::~IPSet()
{
    delete ui;
}

QString IPSet::f_GetIPAddress()
{
    return ui->ui_LineEditIPAddress->text();
}

QString IPSet::f_GetPort()
{
    return ui->ui_LineEditPort->text();
}

void IPSet::on_ui_BtnCancel_clicked()
{
    this->close();
}

void IPSet::on_ui_BtnSubmit_clicked()
{
    if(ui->ui_LineEditPort->text().isEmpty()
            || ui->ui_LineEditIPAddress->text().isEmpty())
    {
        QMessageBox::warning(this, "警告", "信息不完善");
    }
    else
    {
        ui->ui_LineEditPort->setEnabled(false);
        ui->ui_LineEditIPAddress->setEnabled(false);
        this->accept();
    }
}
